package cn.iocoder.yudao.module.shop.revenue.dao;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.shop.revenue.pojo.dto.RevenueQueryDTO;
import cn.iocoder.yudao.module.shop.revenue.pojo.po.ShopRevenue;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * Mapper 接口
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/04/30
 */
@Mapper
public interface RevenueMapper extends CoreMapper<ShopRevenue> {

    @Select("""
            SELECT IFNULL(SUM(settlement_money), 0)
            FROM shop_revenue
            WHERE shop_id = #{shopId} AND status = 0 AND deleted = FALSE
            """)
    Integer selectFreezingSettlementMoney(@Param("shopId") Long shopId);

    @Select("""
            SELECT IFNULL(SUM(settlement_money), 0)
            FROM shop_revenue
            WHERE shop_id = #{shopId} AND unfreeze_time >= CURDATE() AND unfreeze_time < CURRENT_DATE() + INTERVAL 1 DAY AND status = 0 AND deleted = FALSE
            """)
    Integer selectTodayFreezingSettlementMoney(@Param("shopId") Long shopId);

    @Select("""
            SELECT IFNULL(SUM(settlement_money), 0)
            FROM shop_revenue
            WHERE shop_id = #{shopId} AND unfreeze_time >= CURDATE() AND unfreeze_time < CURRENT_DATE() + INTERVAL 1 DAY AND status = 1 AND deleted = FALSE
            """)
    Integer selectTodayUnfreezeSettlementMoney(@Param("shopId") Long shopId);

    @Select("""
            SELECT IFNULL(SUM(settlement_money), 0)
            FROM shop_revenue
            WHERE shop_id = #{shopId} AND status = 1 AND deleted = FALSE
            """)
    Integer selectTotalUnfreezeSettlementMoney(@Param("shopId") Long shopId);

    default List<ShopRevenue> selectByShopIdAndTradeNo(Long shopId, String tradeNo) {
        return selectList(new LambdaQueryWrapper<ShopRevenue>()
                .eq(ShopRevenue::getShopId, shopId)
                .eq(ShopRevenue::getTradeNo, tradeNo));
    }

    default ShopRevenue selectByShopIdAndOrderItemId(Long shopId, Long orderItemId) {
        return selectOne(ShopRevenue::getShopId, shopId, ShopRevenue::getOrderItemId, orderItemId);
    }

    default Integer selectNumberSum(RevenueQueryDTO param) {
        CoreLambdaQueryWrapper<ShopRevenue> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.select(ShopRevenue::getSettlementMoney);
        wrapper.eqIfPresent(ShopRevenue::getStatus, param.getStatus());
        wrapper.eqIfPresent(ShopRevenue::getShopId, param.getShopId());
        wrapper.betweenIfPresent(ShopRevenue::getCreateTime, param.buildCreateTime());
        List<Integer> objs = selectObjs(wrapper, ShopRevenue::getSettlementMoney);
        return objs.stream().reduce(0, Integer::sum);
    }

}
